package com.xieyong.leetcode;

import java.util.Arrays;

public class Leetcode3085 {
    public static void main(String[] args) {
        Leetcode3085 leetcode3085 = new Leetcode3085();
        String word = "dabdcbdcdcd";
        int k = 2;
        int i = leetcode3085.minimumDeletions(word, k);
        System.out.println(i);
    }


    public int minimumDeletions(String word, int k) {
        int[] countlist = new int[26];
        Arrays.fill(countlist, 0);
        for (char c : word.toCharArray()) {
            countlist[c - 'a']++;
        }
        Arrays.sort(countlist);
        int res = Integer.MAX_VALUE;
        for (int value : countlist) {
            if (value == 0) continue;
            int result = 0;
            for (int i : countlist) {
                if (i == 0) continue;
                if (i < value) {
                    result += i;
                } else if (i > value + k) {
                    result += i - (value + k);
                }
            }
            res = Math.min(res, result);
        }
        return res;
    }
}
